package com.erp.sell.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.erp.sell.domain.ErpSalOrderProduct;
import com.erp.sell.domain.ErpSalesOrderDTO;
import com.erp.sell.domain.ErpSalesOrderVO;
import com.erp.sell.mapper.ErpSalOrderProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.erp.sell.mapper.ErpSalesOrderMapper;
import com.erp.sell.domain.ErpSalesOrder;
import com.erp.sell.service.IErpSalesOrderService;

/**
 * 销售订单Service业务层处理
 *
 * @author lzx
 * @date 2025-09-02
 */
@Service
@Slf4j
public class ErpSalesOrderServiceImpl implements IErpSalesOrderService
{
    @Autowired
    private ErpSalesOrderMapper erpSalesOrderMapper;

    @Autowired
    private ErpSalOrderProductMapper erpSalOrderProductMapper;

    /**
     * 查询销售订单列表
     *
     * @param erpSalesOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<ErpSalesOrderVO> selectErpSalesOrderList(ErpSalesOrder erpSalesOrder)
    {
        List<ErpSalesOrderVO> list = erpSalesOrderMapper.selectErpSalesOrderList(erpSalesOrder);

        return list;
    }

    /**
     * 订单数量
     * @return
     */
    @Override
    public int count(ErpSalesOrder erpSalesOrder) {
        return erpSalesOrderMapper.count(erpSalesOrder);
    }

    /**
     * 新增销售订单
     * 数据库表设计考虑不周到,订单id和订单编号没有区分=>添加时[通过新建Mapper接口查询后]手动id自增.
     *
     * @param erpSalesOrderDTO 封装的销售订单需要的字段
     * @return 结果
     */
    @Override
    public int insertErpSalesOrder(ErpSalesOrderDTO erpSalesOrderDTO)
    {
        // 1. 封装销售订单数据
        ErpSalesOrder erpSalesOrder = new ErpSalesOrder();
        // 获取数据库中销售订单的最大id,手动自增id
        Long salesOrderId = erpSalesOrderMapper.total() + 1;
        erpSalesOrder.setOrderId(salesOrderId);
        erpSalesOrder.setUserId(erpSalesOrderDTO.getCustId());
        erpSalesOrder.setTotalAmt(erpSalesOrderDTO.getTotalAmt());
        erpSalesOrder.setOrderTime(new Date());
        erpSalesOrder.setStatus(1);
        log.debug("新增销售订单:{}", erpSalesOrder);
        // 插入销售订单
        erpSalesOrderMapper.insertErpSalesOrder(erpSalesOrder);

        // 2. 销售订单与商品关联表插入数据
        for (Map<String, Long> productDetail : erpSalesOrderDTO.getProductsList()) {
            ErpSalOrderProduct erpSalOrderProduct = new ErpSalOrderProduct();
            erpSalOrderProduct.setOrderId(erpSalesOrder.getOrderId());
            erpSalOrderProduct.setProdId(productDetail.get("productId"));
            erpSalOrderProduct.setCount(productDetail.get("count"));
            log.debug("新增销售订单商品:{}", erpSalOrderProduct);
            erpSalOrderProductMapper.insertErpSalOrderProduct(erpSalOrderProduct);
        }

        return 1;
    }

    /**
     * 修改销售订单[状态改变为提交]
     *
     * @param orderId 销售订单主键
     * @return 结果
     */
    @Override
    public int updateErpSalesOrder(int orderId)
    {
        return erpSalesOrderMapper.updateErpSalesOrder(orderId);
    }


    /**
     * 删除销售订单信息[状态改变为取消]
     *
     * @param orderId 销售订单主键
     * @return 结果
     */
    @Override
    public int deleteErpSalesOrderByOrderId(int orderId)
    {
        return erpSalesOrderMapper.deleteErpSalesOrderByOrderId(orderId);
    }

    /**
     * 获取订单状态统计
     *
     * @return 订单状态统计结果
     */
    @Override
    public List<Map<String, Object>> getOrderStatusStatistics()
    {
        return erpSalesOrderMapper.getOrderStatusStatistics();
    }

}
